#!/bin/sh

INIT=sninit
sbindir=$(grep ^sbindir config.mk | sed -e 's/.*:=\s*//')
s=$(grep "^s :=" Makefile | sed -e 's/.*:=\s*//')
initctl=$(grep '^#define INITCTL ' config.h | sed -e 's/.*"\([^"]\+\)".*/\1/')
inittab=$(grep '^#define INITTAB ' config.h | sed -e 's/.*"\([^"]\+\)".*/\1/')
initdir=$(grep '^#define INITDIR ' config.h | sed -e 's/.*"\([^"]\+\)".*/\1/')
initdir=${initdir:-/etc/initdir}
tabbase=`basename "$inittab"`

logdir=$(grep '^#define LOGDIR' run.h | sed -e 's/.*"\([^"]\+\)".*/\1/')
cgbase=$(grep '^#define CGBASE' run.h | sed -e 's/.*"\([^"]\+\)".*/\1/')

exec sed \
	-e "s!{INIT}!$INIT!g" \
	-e "s!{/sbin}!$sbindir!g" \
	-e "s!{init}!${s}init!g" \
	-e "s!{telinit}!${s}telinit!g" \
	-e "s!{inittab}!$tabbase!g" \
	-e "s!{@initctl}!$initctl!g" \
	-e "s!{/etc/inittab}!$inittab!g" \
	-e "s!{/etc/initdir}!$initdir!g" \
	-e "s!{/var/log}!$logdir!g" \
	-e "s!{/sys/fs/cgroup}!$cgbase!g" \
	"$@"
